const Router = require('koa-router')
const router = new Router({ prefix: '/users' })
const jwt = require('koa-jwt')
const { find, findById, update, create, del, login, checkOwner, listFollowing, follow, unfollow, listFollower, checkUserExist, followTopic, unfollowTopic, listFollowingTopic, listQuestion, followQuestion, unfollowQuestion, listFollowingQuestion, listLikingAnswer, followAnswer, unfollowAnswer, disfollowAnswer, undisfollowAnswer, dislistLikingAnswer, listCollectAnswer, collectAnswer, uncollectAnswer } = require('../controlles/users')
const { secret } = require('../config')
const { checkTopicExist } = require('../controlles/topics')
const { checkQuestionExist } =require('../controlles/question')
const { checkAnswerExist } =require('../controlles/answer')

const auth = jwt({ secret })


router.post('/login',login)

router.get('/', find)


router.get('/:id', findById)



router.post('/', create)



router.patch('/:id', auth, checkOwner, update)



router.delete('/:id', auth, checkOwner, del)

//获取关注列表
router.get('/:id/following', listFollowing)

//获取某人粉丝列表
router.get('/:id/follower', listFollower)

// 人的关注与取消
router.put('/following/:id',checkUserExist, auth, follow)


router.delete('/unfollowing/:id', checkUserExist,auth, unfollow)

// 话题的关注与取消
router.put('/followingTopic/:id', auth, checkTopicExist, followTopic)


router.delete('/unfollowingTopic/:id', auth, checkTopicExist, unfollowTopic)

router.get('/:id/followingTopic', listFollowingTopic)


// 用户问题


// 得到用户创建问题列表
router.get('/:id/question', listQuestion)

router.put('/followingQuestion/:id', auth, checkQuestionExist, followQuestion)


router.delete('/unfollowingQuestion/:id', auth, checkQuestionExist, unfollowQuestion)

router.get('/:id/followingQuestion', listFollowingQuestion)


// 赞和踩
// 赞
router.put('/followingAnswer/:id', auth, checkAnswerExist, followAnswer, undisfollowAnswer )


router.delete('/unfollowingAnswer/:id', auth, checkAnswerExist, unfollowAnswer)

router.get('/:id/followingAnswer', listLikingAnswer)



//  踩

router.put('/disfollowingAnswer/:id', auth, checkAnswerExist, disfollowAnswer, unfollowAnswer )


router.delete('/undisfollowingAnswer/:id', auth, checkAnswerExist, undisfollowAnswer)

router.get('/:id/disfollowingAnswer', dislistLikingAnswer)

// 收藏答案


router.put('/colloectAnswer/:id', auth, checkAnswerExist, collectAnswer )


router.delete('/uncollectAnswer/:id', auth, checkAnswerExist, uncollectAnswer)

router.get('/:id/listCollectAnswer', listCollectAnswer)



module.exports = router